package com.xuecheng.order.task;

import com.xuecheng.order.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 定时任务类
 */
@Component
@Slf4j
public class PaymentJob {

    @Autowired
    private OrdersService ordersService;

    /**
     * 定时指定的java方法，没有返回和参数
     * eg：每5秒，打印系统时间
     * eg：每10分钟，清理超时订单
     */
    @Scheduled(cron = "0/5 * * * * ?")
    //@Scheduled(cron = "0 0/10 * * * ?")
    public void handle() {
        String now = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
        log.info("当前时间：{}",now);
        try {
            //清理超时订单
            ordersService.getPayment();
        } catch (Exception e) {
            log.info(e.getMessage());
        }
    }
}
